CXX=g++
CXXFLAGS=-Wall -Wextra -Winit-self -Wstrict-aliasing=2 -Wfloat-equal -Wno-comment -Wno-missing-field-initializers -Wno-unused -fPIC -DPTRACING=1 -D_REENTRANT -D_GNU_SOURCE=1 -felide-constructors -Wreorder -Iinclude
LDFLAGS=-lpthread
ifeq ($(DEBUG_BUILD),yes)
CXXFLAGS += -ggdb -O0 -DDEBUG -DVERBOSE_DEBUG=1
endif # DEBUG_BUILD
CXXFLAGS += $(CXXFLAGS_ARCH)
LDFLAGS += $(LDFLAGS_ARCH)
ifndef $(ARCH)
ARCH = default
endif

RM=rm
RMFLAGS=-rf
MKDIR=mkdir
AR=ar
ARFLAGS=rcs

SOURCE_DIR=src

BUILD_DIR=build-$(ARCH)

SOURCE_FILES=$(shell cd $(SOURCE_DIR) ; ls *.cxx)

LIBOUT=$(BUILD_DIR)/libcommon.a

OBJ=$(SOURCE_FILES:%.cxx=$(BUILD_DIR)/%.o)

default: debug

release: all

debug:
	$(MAKE) DEBUG_BUILD=yes all
	
all: $(LIBOUT)

$(LIBOUT): init $(OBJ)
	$(AR) $(ARFLAGS) $(LIBOUT) $(OBJ)

$(BUILD_DIR)/%.o: $(SOURCE_DIR)/%.cxx
	$(CXX) -c $(CXXFLAGS) -o $@ $<
	
init:
	@if [ ! -d $(BUILD_DIR) ] ; then $(MKDIR) -p $(BUILD_DIR) ; fi

.PHONY: clean

clean:
	$(RM) $(RMFLAGS) $(BUILD_DIR)

